//
//  movieViewController.m
//  项目一
//
//  Created by nick on 15/7/17.
//  Copyright (c) 2015年 nick. All rights reserved.
//

#import "movieViewController.h"
#import "movieRank.h"
#import "movieCell.h"
#define sheight [UIScreen mainScreen].bounds.size.height
#define swidth [UIScreen mainScreen].bounds.size.width
@interface movieViewController (){
    NSMutableArray *rankDatas;
}

@end

@implementation movieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self _createNav];
    [self _createPosterView];
    [self _createTableView];
    
    //self.view.backgroundColor = [UIColor redColor];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -createSubView
- (void)_createNav{
    UIView *flipView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn1 setImage:[UIImage imageNamed:@"list_home"] forState:UIControlStateNormal];
    [btn1 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    btn1.tag = 1;
    [btn1 sizeToFit];
    [btn1 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [flipView addSubview:btn1];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.tag = 2;
    [btn2 setImage:[UIImage imageNamed:@"poster_home"] forState:UIControlStateNormal];
    [btn2 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    [btn2 sizeToFit];
    [btn2 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [flipView addSubview:btn2];
    btn2.hidden = YES;
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:flipView];
    self.navigationItem.rightBarButtonItem = item;
    

}
- (void)_createPosterView{
    _posterView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _posterView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_posterView];
    
}
- (void)_createTableView{
    _movieTableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    _movieTableView.frame = CGRectMake(0, 0, swidth, sheight - 104);
    _movieTableView.hidden = YES;
    _movieTableView.dataSource = self;
    _movieTableView.delegate = self;
    [self.view addSubview:_movieTableView];
    
    //注册 单元格
    UINib *nib = [UINib nibWithNibName:@"movieCell" bundle:[NSBundle mainBundle]];
    [_movieTableView registerNib:nib forCellReuseIdentifier:@"cell"];
    

}
#pragma mark - action
-(void)btnAction:(UIButton*)btn{
    UIView *flipView = self.navigationItem.rightBarButtonItem.customView;
    UIButton *btn1 = (UIButton*)[flipView viewWithTag:1];
    UIButton *btn2 = (UIButton*)[flipView viewWithTag:2];

    BOOL isleft = btn1.hidden;
    [self flipView:flipView isLeft:isleft];


    
    [self flipView:self.view isLeft:isleft];
    _posterView.hidden = !_posterView.hidden;
    _movieTableView.hidden = !_movieTableView.hidden;
    btn1.hidden = !btn1.hidden;
    btn2.hidden = !btn2.hidden;
    
}

#pragma mark - tools
-(void)flipView:(UIView*)view isLeft:(BOOL)isLeft{
    UIViewAnimationOptions option = isLeft?UIViewAnimationOptionTransitionFlipFromLeft:UIViewAnimationOptionTransitionFlipFromRight;
    [UIView transitionWithView:view duration:0.3 options:option animations:nil completion:nil];
}
#pragma mark - loadData
-(void)loadData{
    
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"us_box" ofType:@"json"];
    //直接存储二进制数据
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *subject = [dic valueForKey:@"subjects"];
    //NSLog(@"%lu",(unsigned long)subject.count);
    rankDatas = [[NSMutableArray alloc]init];
    
    for (int i = 0; i < subject.count; i++) {
        movieRank *rankData = [movieRank new];
        NSDictionary *sub = [subject[i] objectForKey:@"subject"];
        rankData.year = [sub objectForKey:@"year"];
        rankData.images = [sub objectForKey:@"images"];
        NSNumber *number = [[sub objectForKey:@"rating"]objectForKey:@"average"];
        rankData.average = [number floatValue];
        rankData.title = [sub objectForKey:@"title"];
        //NSLog(@"%@",rankData.title);
        [rankDatas addObject:rankData];
    }
//    movieRank *r = rankDatas[3];
//    NSLog(@"%@",r.title);
}
#pragma mark - UItabledatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return rankDatas.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    movieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"forIndexPath:indexPath];
   // cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
//    if (cell == nil) {
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
//    }
    
//    NSLog(@"%@",modal.year);
    cell.modal = rankDatas[indexPath.row];
    cell.selectionStyle = NO;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
